package com.jiuzhi.app.common.wxutil.model;

import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageUtil {
    public static final String MESSAGE_TEXT = "text";
    public static final String MESSAGE_IMAGE = "image";
    public static final String MESSAGE_VOICE = "voice";
    public static final String MESSAGE_VIDEO = "video";
    public static final String MESSAGE_SHORTVIDEO = "shortvideo";
    public static final String MESSAGE_LINK = "link";
    public static final String MESSAGE_LOCATION = "location";
    public static final String MESSAGE_EVENT = "event";
    public static final String MESSAGE_SUBSCRIBE = "subscribe";
    public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
    public static final String MESSAGE_CLICK = "CLICK";
    public static final String MESSAGE_VIEW = "VIEW";
    public static final String MESSAGE_SCAN = "SCAN";

    /**
     * 将XML转为MAP集合
     *
     * @param request
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
        Map<String, String> map = new HashMap<String, String>();
        SAXReader reader = new SAXReader();
        //从request对象中获取输入流
        InputStream ins = request.getInputStream();
        //使用reader对象读取输入流,解析为XML文档
        Document doc = reader.read(ins);
        //获取XML根元素
        Element root = doc.getRootElement();
        //将根元素的所有节点，放入列表中
        List<Element> list = root.elements();
        //遍历list对象，并保存到集合中
        for (Element element : list) {
            map.put(element.getName(), element.getText());
        }
        ins.close();
        return map;
    }

    /**
     * 解析微信发来的请求（XML）
     * xml示例
     *
     * @param request
     * @return
     * @throws Exception
     */
    public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
        // 将解析结果存储在HashMap中
        Map<String, String> map = new HashMap<String, String>();
        // 从request中取得输入流
        InputStream inputStream = request.getInputStream();
        // 读取输入流
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        // 得到xml根元素
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();
        // 遍历所有子节点
        for (Element e : elementList)
            map.put(e.getName(), e.getText());
        // 释放资源
        inputStream.close();
        inputStream = null;
        return map;
    }


    /**
     * 将文本消息对象转成XML
     *
     * @param text
     * @return
     */
    public static String textMessageToXml(TextMessage textMessage) {
        XStream xstream = new XStream();
        //将xml的根节点替换成<xml>  默认为TextMessage的包名
        xstream.alias("xml", textMessage.getClass());
        return xstream.toXML(textMessage);
    }

    /**
     * 拼接关注主菜单
     */
    public static String menuText() {
        StringBuffer sb = new StringBuffer();
        sb.append("您好，欢迎关注亚新物业，畅享美好生活！亚新物业客服热线：400-188-9999。");
//		sb.append("测试关注回复功能。\n");
        return sb.toString();
    }

    /**
     * 初始化回复消息
     */
    public static String initText(String toUSerName, String fromUserName, String content) {
        TextMessage text = new TextMessage();
        text.setFromUserName(toUSerName);
        text.setToUserName(fromUserName);
        text.setMsgType(MESSAGE_TEXT);
        text.setCreateTime(new Date().getTime() + "");
        text.setContent(content);
        return MessageUtil.textMessageToXml(text);
    }

    public static String initImg(String toUSerName, String fromUserName, String url) {
        TextMessage text = new TextMessage();
        text.setFromUserName(toUSerName);
        text.setToUserName(fromUserName);
        text.setMsgType(MESSAGE_IMAGE);
        text.setCreateTime(new Date().getTime() + "");
        text.setContent(url);

        Map<String, Object> bodyMap = new HashMap<String, Object>();
        bodyMap.put("Title", "");
        bodyMap.put("Description", "");
        bodyMap.put("PicUrl", "http://image.so.com/v?q=%E7%99%BE%E5%BA%A6%E5%9B%BE%E7%89%87&src=tab_www&correct=%E7%99%BE%E5%BA%A6%E5%9B%BE%E7%89%87&cmsid=faec2e237a2cd0dc6a46cf7c4f819e80&cmran=0&cmras=0&cn=0&gn=0&kn=22#q=%E7%99%BE%E5%BA%A6%E5%9B%BE%E7%89%87&src=tab_www&correct=%E7%99%BE%E5%BA%A6%E5%9B%BE%E7%89%87&cn=0&gn=0&kn=22&lightboxindex=17&id=3350a7da4582fecaa944228726269bce&multiple=0&itemindex=0&dataindex=17&prevsn=0&currsn=0&jdx=17&fsn=82&gsrc=1");
        bodyMap.put("Url", "");


        return MessageUtil.textMessageToXml(text);
    }
}
